var ADMIN_FORM = {
		isChecked: true,
		linkAction: '',
		init:function(){
			$('table.table-form td.input span.error').hide();
			$('table.table-form td.input > input#button_submit').click(function(){
				/*
				 * check form require value
				 */
				$('input[data-require=not-empty], textarea[data-require=not-empty]').each(function(){
					if($(this).val()==''){
						ADMIN_FORM.isChecked = false;
						$(this).next('span.error').show();
					}else{
						$(this).next('span.error').hide();
					}
				});
				/*
				 * check form require selected
				 */
				$('select[data-require=selected]').each(function(){
					if(!$(this).val() || parseInt($(this).val(),10) < 0 ){
						ADMIN_FORM.isChecked = false;
						$(this).next('span.error').show();
					}else{
						$(this).next('span.error').hide();
					}
				});
				if(ADMIN_FORM.isChecked){
					$('#form_main').submit();
				}else{
					ADMIN_FORM.isChecked = true;
				}
				
			});
			$('input.number').keydown(function(event) {
				// Allow: backspace, delete, tab, escape, and enter,  Ctrl+A, home, end, left, right
				if ( event.keyCode ==190 || event.keyCode == 110 ||  event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || (event.keyCode == 65 && event.ctrlKey === true) || (event.keyCode >= 35 && event.keyCode <= 39)) {
					return;
				}
				else {
					// Ensure that it is a number and stop the keypress
					if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
						event.preventDefault(); 
					}
				}
			});
			$('select.refer-to').change(function(){
				$('#'+$(this).attr('data-refer')).val($(this).val());
			});
			$('input[type=checkbox].multi.refer-to').click(function(){
				var sepa = $(this).attr('data-separator');
				var obj = $('#'+$(this).attr('data-refer'));
				var val = $(this).attr('data-add-first') + $(this).val() + $(this).attr('data-add-last');
				if($(this).is(':checked')){
					if(obj.val()==''){
						obj.val(sepa);
					}
					var str = obj.val() + val + sepa;
					obj.val(str);
				}else{
					var str = obj.val()+'';
					str = str.replace(sepa + val + sepa+'',sepa);
					obj.val(str);
					if(obj.val()==sepa){
						obj.val('');
					}
				}
			});
			ADMIN_FORM.initSelectParent();
		},
		initSelectParent:function(){
			$('select.select-parent').each(function(){
				var obj = $(this);
				var id = obj.attr('data-child-id');
				var urlUpdate = obj.attr('data-update');
				var require = obj.attr('data-child-require');
				var it = obj.attr('data-child-select');
				var cls =  obj.attr('data-child-class');
				var more =  obj.attr('data-child-more');
				if(id && urlUpdate){
					var chl = $('#'+id);
					var str = '<select name="tmp_'+id+'" id="tmp_'+id+'" data-require="'+require+'" class="'+cls+'" '+more+'>'
							+ '<option value="-1">'+it+'</option></select>';
					chl.parent().append(str);
					obj.change(function(){
						$('#'+id).val('-1');
						$.ajax({
			                type: "POST",
			                url: urlUpdate,
			                data: {
			                	cate_id: $(this).val()
			                },
			                cache: false,
			                success: function(data){
			                	data = $.parseJSON(data);
			                	if(data.status==1){
			                		$('select#tmp_'+id).html('<option value="-1">...loading...</option>');
			                		var str='<option value="-1" selected>'+it+'</option>';
			                		if(data.data.length){
			                			for(var i=0; i<data.data.length; i++){
			                				str+='<option value="'+data.data[i].id+'">'+data.data[i].name+'</option>';
			                			}
			                		}
			                		$('select#tmp_'+id).html('');
			                		$('select#tmp_'+id).append(str);
			                	}else{
			                		alert('Error when trying connect to server. Contac with vcqviet@gmail.com');
			                	}
			                }
			            });
					});
				}
				if(parseInt(obj.val(),10)>0){
					$.ajax({
		                type: "POST",
		                url: urlUpdate,
		                data: {
		                	cate_id: obj.val()
		                },
		                cache: false,
		                success: function(data){
		                	data = $.parseJSON(data);
		                	if(data.status==1){
		                		$('select#tmp_'+id).html('<option value="-1">...loading...</option>');
		                		var str='<option value="-1" selected>'+it+'</option>';
		                		
		                		if(data.data.length){
		                			for(var i=0; i<data.data.length; i++){
		                				str+='<option value="'+data.data[i].id+'" '+(parseInt($('#'+id).val(),10)==parseInt(data.data[i].id,10)?'selected':'')+'>'+data.data[i].name+'</option>';
		                			}
		                		}
		                		$('select#tmp_'+id).html('');
		                		$('select#tmp_'+id).append(str);
		                	}else{
		                		alert('Error when trying connect to server. Contac with vcqviet@gmail.com');
		                	}
		                }
		            });
					
				}
				$('select#tmp_'+id).change(function(){
					$('#'+id).val($(this).val());
        		});
			});
		},
		listLinked:function(val,val_links){
			dataString = 'type_id='+val;
			$.ajax({
                type: "POST",
                url: ADMIN_FORM.linkAction,
                data: dataString,
                cache: false,
                success: function(msg){
                	var sel = document.getElementById('AmountLinks_linked_id');
                	sel.options.length = 0;
                	var data = $.parseJSON(msg);
					for(var i=0;i<data.data.length;i++){
						var option_s=null;
						if(parseInt(data.data[i].id) == parseInt(val_links)){
							option_s = new Option(data.data[i].name,data.data[i].id,true,true);
						}else{
							option_s = new Option(data.data[i].name,data.data[i].id);
						}
						sel.options.add(option_s);
					}
                }
            });
		}
}